/*
 * FileName：ImgResourceCtrl.java 
 * <p>
 * Copyright (c) 2017-2020 <a href="https://www.smartwx.info">hermit(1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.wx.wxcms.ctrl;

import com.google.common.collect.Maps;
import com.smart.common.BaseCtrl;
import com.smart.common.util.AjaxResult;
import com.smart.common.util.UploadUtil;
import com.smart.common.util.WxMemoryCacheClient;
import com.smart.wx.wxapi.process.WxApiClient;
import com.smart.wx.wxcms.entity.ImgResource;
import com.smart.wx.wxcms.service.ImgResourceService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/** 
 * @author : hermit
*/
@Controller
@RequestMapping("/managerImg")
public class ImgResourceCtrl extends BaseCtrl {

	private Logger logger=LoggerFactory.getLogger(ImgResourceCtrl.class);
	@Autowired
	private ImgResourceService imgResourceService;

    @RequestMapping(value = "/list")
    @ResponseBody
    public AjaxResult list(ImgResource searchEntity) {
    	logger.info("获取图片分页数据");
    	List<ImgResource> pageList = imgResourceService.getImgListByPage(searchEntity);
        return getResult(searchEntity, pageList);
    }
	
	/**
	 * 上传图片(富文本)
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("uploadFile")
	public Map uploadFile(MultipartFile file) throws Exception {
		//上传
		Map<String, String> stringMap = UploadUtil.uploadFile(file);
		//构造返回参数
		Map<String, Object> map = Maps.newHashMap();
		Map<String, Object> mapData = Maps.newHashMap();
		map.put("code", 0);//0表示成功，1失败
		map.put("msg", "上传成功");//提示消息
		map.put("data", mapData);//提示消息
		mapData.put("src", stringMap.get(UploadUtil.RES_URL));//图片url
		mapData.put("title", stringMap.get(UploadUtil.FILE_NAME));//图片名称，这个会显示在输入框里
		return map;
	}

	/**
	 * 上传图片(普通上传)
	 *
	 * @return
	 */
	@ResponseBody
	@RequestMapping("uploadFileImg")
	public AjaxResult uploadFileImg(MultipartFile file) throws Exception {
		//上传
		Map<String, String> stringMap = UploadUtil.uploadFile(file);
		return AjaxResult.success(stringMap.get(UploadUtil.RES_URL));
	}
	/**
	 *  删除图片
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("delMediaImg")
	public AjaxResult delMediaImg(String id)throws Exception {
		logger.info("删除图片 id：{}",id);
		ImgResource img = imgResourceService.getImg(id);
        WxApiClient.deleteMaterial(img.getMediaId(), WxMemoryCacheClient.getMpAccount());
		imgResourceService.delImg(id);
		return AjaxResult.deleteSuccess();
	}
}
